import { defineStore } from "pinia";
import { getAuthorization, setAuthorization } from "@/utils/auth.js"

export const useAccountStore = defineStore("imChat", {
  state: () => {
    return {
      chatListAll:[],
      makeFriendsList:[],
    };
  },
  actions: {
    addChatMsg(item) {
      let idServer = item.idServer
      let idServerArr = this.chatListAll.map(o => o.idServer)
      if(idServerArr.indexOf(idServer) == -1){
        this.chatListAll.push(item)
        setAuthorization('chatListAll',this.chatListAll)
      }
    },
    addMakeFriends(item) {
        let arr = this.makeFriendsList.map(o => o.ID)
        console.log(arr,item.ID,'6696699')
        if(!arr.includes(item.ID)){
            this.makeFriendsList.unshift(item)
        }else{
            let index = arr.findIndex( o => o == item.ID)
            this.makeFriendsList.unshift(item)
            this.makeFriendsList.splice(index,1)
        }
        setAuthorization('makeFriendsList',this.makeFriendsList)
      },
    setChatMsg(item){
        this.chatListAll = [...item]
    },
    setMakeFriends(item){
        this.makeFriendsList = [...item]
    },
    clearChatMsg(ID){
        let _ID = ID+""
        for (let i = this.chatListAll.length - 1; i >= 0; i--) {
            if ((this.chatListAll[i].chatType == 2 && this.chatListAll[i].to == _ID) || (this.chatListAll[i].chatType == 1 && this.chatListAll[i].from == _ID)) {
                this.chatListAll.splice(i, 1);
            }
        }
        setAuthorization('chatListAll',this.chatListAll)
    },
    // 修改状态为已读
    updateIsRead(userIMId){
      this.chatListAll.forEach(o=>{
        if(o.chatType == 1 && o.from == userIMId){
          o.isRead = 2
        }
      })
      setAuthorization('chatListAll',this.chatListAll)
    }
  },
});